import 'package:citiesocial/_include.dart';
import 'package:citiesocial/ui/account/my_address_add.dart';
import 'package:flutter_slidable/flutter_slidable.dart';

class MyAddress extends BaseStatefulWidget {
  final selectable;
  MyAddress([this.selectable = false]);
  @override
  State<StatefulWidget> createState() => _MyAddressState();
}

class _MyAddressState extends BaseState<MyAddress> {
  final _list = List<AddressEntity>();
  _MyAddressState();

  void _onMenuTap() {
    Navigator.pushNamed(context, '/address/add').then((_) {
      _loadAddress();
    });
  }

  void _default(AddressEntity address) {
    if (address.is_main != '1') {
      HttpManager.updateMainAddress(
              Configs.userId, Configs.userToken, address.address_id)
          .then((yes) {
        if (yes) {
          //Toast.show(context, '修改成功');
          _loadAddress();
        } else {
          Toast.show(context, '修改失败');
        }
      });
    }
  }

  void _delete(AddressEntity address) {
    HttpManager.deleteAddress(
            Configs.userId, Configs.userToken, address.address_id)
        .then((yes) {
      if (yes) {
        //Toast.show(context, '删除成功');
        _loadAddress();
      } else {
        Toast.show(context, '删除失败');
      }
    });
  }

  void _loadAddress() {
    LoadingDialog.show(context);
    HttpManager.addressList(Configs.userId, Configs.userToken, context: context)
        .then((list) {
      LoadingDialog.dismiss(context);
      _list.clear();
      _list.addAll(list);
      setState(() {});
    });
  }

  @override
  void initState() {
    Future.delayed(Configs.loadDelay, () {
      _loadAddress();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBarUtils.normal(
            context,
            '常用收件地址',
            GestureDetector(
              onTap: _onMenuTap,
              child: Container(
                padding: AppBarUtils.inner_image_padding,
                child: Image.asset(
                  'imgs/js_navbars_img_add.png',
                  fit: BoxFit.scaleDown,
                ),
              ),
            )),
        backgroundColor: Colors.white,
        body: SafeArea(
          child: DListView(
            onRefresh: () async {
              _loadAddress();
            },
            itemCount: _list.length,
            itemBuilder: (_, index) => GestureDetector(
                  child: Slidable(
                    actionExtentRatio: 0.25,
                    delegate: SlidableDrawerDelegate(),
                    child: ItemMenu2(
                      title: _list[index].is_main == '1'
                          ? '${_list[index].name} (主要)'
                          : '${_list[index].name}',
                      subtitle: '${_list[index].address}',
                      rightIcon: 'imgs/js_tabs_secondscreen_img_disclosure.png',
                    ),
                    secondaryActions: <Widget>[
                      IconSlideAction(
                        caption: '默认',
                        color: _list[index].is_main == '1'
                            ? Colors.grey
                            : Colors.blueAccent,
                        icon: Icons.lock,
                        onTap: () => _default(_list[index]),
                      ),
                      IconSlideAction(
                        caption: '删除',
                        color: Colors.red,
                        icon: Icons.delete,
                        onTap: () => _delete(_list[index]),
                      )
                    ],
                  ),
                  onTap: () {
                    if (!widget.selectable) {
                      Navigator.push(
                          context,
                          MaterialPageRoute<String>(
                              builder: (_) => MyAddressAdd(
                                    _list[index].address_id,
                                  ))).then((_) {
                        _loadAddress();
                      });
                    } else {
                      Navigator.maybePop(context, _list[index]);
                    }
                  },
                ),
          ),
        ),
      );
}
